<template>
    <el-alert show-icon style="margin-bottom: 20px;" effect="dark" title="启用快速生成时,无需填写其他参数!"
        description="快速生成根据实体批量生成业务模板!" type="success" :closable="false" />

    <el-form :model="form" label-width="auto">

        <el-form-item label="文件命名空间">
            <el-input :disabled="form.fastAuto" v-model="form.nameSpace" placeholder="文件命名空间" />
        </el-form-item>

        <el-form-item label="输出相对路径">
            <el-input :disabled="form.fastAuto" v-model="form.path" placeholder="输出相对路径" />
        </el-form-item>

        <el-form-item label="指定实体名称">
            <el-select :disabled="form.fastAuto" v-model="form.modelNames" multiple placeholder="指定实体名称">
                <el-option v-for="item in modelNames" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
        </el-form-item>

        <el-form-item label="模板类型">
            <el-select :disabled="form.fastAuto" v-model="form.fileTemplateType" placeholder="模板类型">
                <el-option v-for="item in FileTemplateType" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
        </el-form-item>

        <el-form-item label="全部快速生成">
            <el-switch @change="handleFastAutoChange" v-model="form.fastAuto" />
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="onSubmit" :loading="loading">
                <template #icon>
                    <Icon icon="line-md:document-code"></Icon>
                </template>
                生成
            </el-button>
        </el-form-item>

    </el-form>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { Icon } from '@iconify/vue';
import { CreateFilesByModels, GetModelNames } from '../../../utils/api';

const modelNames = ref([])
const loading = ref(false)
const form = ref({
    nameSpace: '',
    path: '',
    modelNames: [],
    fileTemplateType: '',
    fastAuto: true,
})
const FileTemplateType = [
    {
        value: 0,
        label: '仓储接口层',
    }, {
        value: 1,
        label: '仓储实现层',
    }, {
        value: 2,
        label: '服务接口层',
    }, {
        value: 3,
        label: '服务实现层',
    }, {
        value: 4,
        label: 'API控制器层',
    },
]

const handleFastAutoChange = async (value) => {
    if (value == true) {
        form.value.nameSpace = ''
        form.value.path = ''
        form.value.modelNames = []
        form.value.fileTemplateType = ''
    } else {
        await HaveModelNames();
    }
}

const onSubmit = async () => {
    ElMessageBox.confirm('是否生成ViewModel实体文件?', '提示',
        {
            confirmButtonText: '是',
            cancelButtonText: '否',
            type: 'warning',
        }
    ).then(async () => {
        if (form.value.fastAuto == false) {
            if (!form.value.nameSpace) {
                ElMessage.warning('文件命名空间不能为空');
                return;
            } else if (!form.value.path) {
                ElMessage.warning('输出相对路径不能为空');
                return;
            } else if (!form.value.modelNames || form.value.modelNames.length === 0) {
                ElMessage.warning('实体名称不能为空');
                return;
            } else if (form.value.fileTemplateType === '') {
                ElMessage.warning('模板类型不能为空');
                return;
            }
        }

        loading.value = true;
        try {
            await CreateFilesByModels(form.value);
        } finally {
            loading.value = false;
        }
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '取消生成',
        })
    })
}

const HaveModelNames = async () => {
    if (modelNames.value.length == 0) {
        var result = await GetModelNames();
        modelNames.value = result.map(x => ({
            value: x,
            label: x
        }))
    }
}

onMounted(async () => {

})
</script>